<?php
/**
*微信接口相关开发
*
*/

class wxAuthLib
{
	public $encodingAesKey = "";
	public $token = "";
	public $appId = "";
	public $appSecret = "";
	public $redirectUrl = "";
	public $helper;
	public function __construct()
	{
		include_once "wxHelper.php";
		$wxData = include_once "wxConfig.php";
		$this->encodingAesKey = $wxData['SecretKey'];
		$this->token = $wxData['Token'];
		$this->appId = $wxData['AppId'];
		$this->appSecret = $wxData['AppSecret'];
		$this->redirectUrl = $wxData['RedirectUrl'];
		$this->helper = new wxHelper();
	}
	/*
	*获取component_access_token
	*/
	public function getComponentAccessToken()
	{
		$data = json_decode($this->helper->get_php_file(dirname(__FILE__)."/static/component_access_token.php"), true);
		if ($data['expire_time'] < time()) {
	      $url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";

	      $curlPost['component_appid'] = $this->appId;
		  $curlPost['component_appsecret'] = $this->appSecret;
		  //获取ticket
		  $dataTicket = json_decode($this->helper->get_php_file(dirname(__FILE__)."/static/component_verify_ticket.php"),true);
		  $curlPost['component_verify_ticket'] = $dataTicket['component_verify_ticket'];
		  $curlPost = json_encode($curlPost);

	      $res = json_decode($this->helper->httpPost($curlPost, $url), true);
	      $component_access_token = $res['component_access_token'];
	      if ($component_access_token) {
	        $filedata['expire_time'] = time() + 7000;
	        $filedata['component_access_token'] = $component_access_token;
	        $this->helper->set_php_file(dirname(__FILE__)."/static/component_access_token.php", json_encode($filedata));
	      }
	    } else {
	      $component_access_token = $data['component_access_token'];
	    }
	    return $component_access_token;

	}

	/*
	*获取pre_auth_code
	*/
	public function getPreAuthCode()
	{
		$data = json_decode($this->helper->get_php_file(dirname(__FILE__)."/static/pre_auth_code.php"), true);
		
		if ($data['expire_time'] < time()) {
		  $component_access_token = $this->getComponentAccessToken();
		
	      $url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=".$component_access_token;

	      $curlPost['component_appid'] = $this->appId;
	      $curlPost = json_encode($curlPost);
		  
	      $res = json_decode($this->helper->httpPost($curlPost, $url), true);
	      $pre_auth_code = $res['pre_auth_code'];
	      if ($pre_auth_code) {
	        $filedata['expire_time'] = time() + 500;
	        $filedata['pre_auth_code'] = $pre_auth_code;
	        $this->helper->set_php_file(dirname(__FILE__)."/static/pre_auth_code.php", json_encode($filedata));
	      }
	    } else {
	      $pre_auth_code = $data['pre_auth_code'];
	    }
	    return $pre_auth_code;
	}
	/*
	*使用授权码换取公众号的接口调用凭据和授权信息
	*/
	public function getAuthorizationAppid($authorization_code)
	{
	  $component_access_token = $this->getComponentAccessToken();
      $url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=".$component_access_token;

      $curlPost['component_appid'] = "wx4f9336afa67947ea";
      $curlPost['authorization_code'] = $authorization_code;
      $curlPost = json_encode($curlPost);
	  
      $res = json_decode($this->helper->httpPost($curlPost, $url), true);
      $authorizer_appid = $res ['authorization_info'] ['authorizer_appid'];
      if($authorizer_appid != "")
      {
      	return $res;
      }else
      {
      	return "";
      }
      

	}
	/*
	*获取（刷新）授权公众号的接口调用凭据（令牌）
	*/
	public function get_authorizer_refresh_token($authorizer_refresh_token, $authorizer_appid)
	{
		$component_access_token = $this->getComponentAccessToken();
      	$url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=".$component_access_token;
      	$curlPost['component_appid'] = $this->appId;
      	$curlPost['authorizer_appid'] = $authorizer_appid;
      	$curlPost['authorizer_refresh_token'] = $authorizer_refresh_token;

      	$curlPost = json_encode($curlPost);
      	$res = json_decode($this->helper->httpPost($curlPost, $url), true);
      	$authorizer_access_token = $res ['authorizer_access_token'];
	      if($authorizer_access_token != "")
	      {
	      	return $res;
	      }else
	      {
	      	return "";
	      }
	}
	/*
	*获取授权方的公众号帐号基本信息
	*/
	public function getAuthorizationInfo($authorizer_appid)
	{
		$component_access_token = $this->getComponentAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=".$component_access_token;
		$curlPost['component_appid'] = $this->appId;
	    $curlPost['authorizer_appid'] = $authorizer_appid;
	    $curlPost = json_encode($curlPost);
		  
	    $res = json_decode($this->helper->httpPost($curlPost, $url), true);
	    if(!empty($res['authorizer_info']))
	    {
	    	return $res;
	    }else
	    {
	    	return "";
	    }
	}

	

	public function getHrefParam()
	{
		$data['component_appid'] = $this->appId;
		$data['pre_auth_code'] = $this->getPreAuthCode();
		$data['redirect_uri'] = $this->redirectUrl;
		return $data;
	}
	
}
?>